package com.yuanfeng.action.admin.admin.web;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanfeng.pojo.Admins;
import com.yuanfeng.util.MD5Util;
import com.yuanfeng.action.admin.admin.service.AdminService;
import com.yuanfeng.util.WebUtil;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;

/**
 * 管理员action
 */
public class AdminAction extends ActionSupport {
	/**
	 * 管理员的业务逻辑依赖di注入
	 */
	private AdminService adminServiceImpl;

	public void setAdminServiceImpl(AdminService adminServiceImpl) {
		this.adminServiceImpl = adminServiceImpl;
	}

	/**
	 * 管理员登陆
	 * @return
	 */
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Admins admin = WebUtil.copyRequestToBean(request, Admins.class);
		Admins loginAdmin = adminServiceImpl.login(admin.getName());
		if(loginAdmin!=null){
			System.out.println("数据库:"+loginAdmin.getPassword());
			System.out.println( "前台:"+MD5Util.md5(admin.getPassword().trim()));
			if(  MD5Util.md5(admin.getPassword().trim()).equals(loginAdmin.getPassword()) ){
				request.getSession().setAttribute("admin",admin);
				//转到管理人员主页
				//重定向
				return "login";
			}else{
				request.setAttribute("msg", "密码输入有误！");
				return ERROR;
			}
		}else{
			request.setAttribute("msg", "用户名不存在！");
			return ERROR;
		}
	}

	/**
	 * 管理员账号注册
	 * @return
	 */
	public String  register(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//得到前台的输入信息
		Admins admins = WebUtil.copyRequestToBean(request, Admins.class);
		adminServiceImpl.registerAdmins(admins);
		return "register";
	}

}
